package com.sxkj.gis.datastorage.shapefiles.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Service;

import com.sxkj.gis.datastorage.shapefile.bean.ShapeFileInfo;
import com.sxkj.gis.datastorage.shapefiles.service.DataStorageService;

@Service("dataStorageService")
public class DataStorageServiceImpl implements DataStorageService {

	@Override
	public List<ShapeFileInfo> getFilesDir(String target, String basePath) {
		
		List<ShapeFileInfo> sfi = new ArrayList<>();
		File file = new File(basePath+target);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		if(file.exists()){
			if(file.isDirectory()){
				File[] fileList = file.listFiles();
				for(int i=0;i<fileList.length;i++){
					ShapeFileInfo shapeFileInfo = new ShapeFileInfo();
					cal.setTimeInMillis(fileList[i].lastModified());
					shapeFileInfo.setFileName(fileList[i].getName()+"/");
					shapeFileInfo.setLastUpdateTime(sdf.format(cal.getTime()));
					sfi.add(shapeFileInfo);
				}
			}else{
				ShapeFileInfo shapeFileInfo = new ShapeFileInfo();
				cal.setTimeInMillis(file.lastModified());
				shapeFileInfo.setFileName(file.getName()+"/");
				shapeFileInfo.setLastUpdateTime(sdf.format(cal.getTime()));
				sfi.add(shapeFileInfo);
			}
		}
		
		return sfi;
	}

	

}
